﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KitBuildingWF.Views
{
    public partial class dbwindow : Form
    {
        private MainWindow mw = null;
        private DataTable dtable = null;
        private DataGridViewSelectedRowCollection selectedRows = null;

        public dbwindow()
        {
            InitializeComponent();
            selectedRows = null;
        }

        public dbwindow(string dbname)
        {
            selectedRows = null;
        }

        private void dbRowSelectBtn_Click(object sender, EventArgs e)
        {
            // Get the selected row

            if (mw == null)
            {
                mw = Program.GetMainWindow();
            }

            // Set the return object
            selectedRows = dbRowsPanel.SelectedRows;

            this.Close();
            //MessageBox.Show(dbRowsPanel.SelectedRows.Count.ToString());
        }

        private void dbwindow_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Set the database source for the datagrid panel
        /// </summary>
        /// <param name="dbtable"></param>
        public void SetDBDatasource(DataTable dbtable){
            dbRowsPanel.DataSource = dbtable;
            this.dtable = dbtable;
        }

        /// <summary>
        /// 
        /// Hide the given column
        /// </summary>
        /// <param name="columnName"></param>
        public void HideColumn(string columnName)
        {
            dbRowsPanel.Columns[columnName].Visible = false;
        }
        

        public DataGridView GetDataGridView()
        {
            return dbRowsPanel;
        }

        /// <summary>
        /// selected rows are set by clicking "Select" button.
        /// </summary>
        /// <returns>selected rows</returns>
        public DataGridViewSelectedRowCollection GetSelectedRows()
        {
            return selectedRows;

        }

        /// <summary>
        /// return the datatable which maintain the structure of the input datatable
        /// </summary>
        public DataTable GetSelectedRowsAsTable()
        {
            DataTable returnDT = new DataTable();

            returnDT = dtable.Copy();
            returnDT.Clear();

            int rowCount = 0;

            if (selectedRows == null)
            {
                return null;
            }

            foreach (DataGridViewRow row in selectedRows)
            {
                returnDT.Rows.Add();
                foreach (DataGridViewCell cell in row.Cells){
                    returnDT.Rows[rowCount][cell.ColumnIndex] = cell.Value;
                }
                rowCount = rowCount +1;
            }

            return returnDT;
        }

        private void dbRowsPanel_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


        /// <summary>
        /// select and return a single kit
        /// </summary>
        private void dbRowsPanel_DoubleClick(object sender, EventArgs e)
        {
            // Set the return object
            selectedRows = dbRowsPanel.SelectedRows;
            
            this.Close();
        }

        private void dbWindowSearchBtn_Click(object sender, EventArgs e)
        {
            SearchForm searchForm = new SearchForm();
            searchForm.ShowDialog();

            // Process the search text
            if (searchForm.GetSearchText() != null)
            {
                //MessageBox.Show(searchForm.GetSearchText());
            }
        }

    }
}
